package com.softserveinc.moviedbapp.map;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;

import com.softserveinc.moviedbapp.R;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class CinemaInfoProvider {

	private static final String TAG = CinemaInfoProvider.class.getSimpleName();

	public static Integer sCinemaId;

	public static Map<Integer, ArrayList<String>> readCinemaInfo( final Context context ) {
		Resources resources = context.getResources();
		Map<Integer, ArrayList<String>> cinemas = new HashMap<Integer, ArrayList<String>>();
		try {
			InputStream rawResourceInputStream = resources.openRawResource( R.raw.cinemas_info );
			BufferedReader reader = new BufferedReader( new InputStreamReader( rawResourceInputStream ) );

			String line = "";
			int counter = 0;
			while ( ( line = reader.readLine() ) != null ) {
				ArrayList<String> cinema = new ArrayList<String>();
				cinema.addAll( Arrays.asList( line.split( "/" ) ) );
				cinemas.put( counter, cinema );
				counter++;
			}

		} catch ( Resources.NotFoundException e ) {
			Log.e( TAG, "Did not find raw resource: ", e );
		} catch ( IOException e ) {
			Log.e( TAG, "Couldn't open the file ", e );
		}

		return cinemas;
	}

	public static void saveSeatMap( final Context context, final HashMap<Integer, Boolean> bookingMap,
			final HashMap<Integer, Boolean> curSessionBookingMap ) {

		File file = new File( context.getCacheDir(), "seatmap" + sCinemaId.toString() );

		FileWriter fileWriter;
		BufferedWriter writer = null;
		try {
			fileWriter = new FileWriter( file );
			writer = new BufferedWriter( fileWriter, 2048 );

			Iterator<Integer> iterator = bookingMap.keySet().iterator();

			while ( iterator.hasNext() ) {
				Integer key = iterator.next();

				Boolean bookingMapBoolean = bookingMap.get( key );
				Boolean curSessionBookingMapBoolean = curSessionBookingMap.get( key );

				Log.d( TAG, "key = " + key.toString() + " bookingMapBoolean = " + bookingMapBoolean.toString() );
				if ( curSessionBookingMapBoolean != null ) {
					Log.d( TAG, "key = " + key.toString() + " curSessionBookingMapBoolean = "
							+ curSessionBookingMapBoolean.toString() );
				} else {
					Log.d( TAG, "key = " + key.toString() + " curSessionBookingMapBoolean = " + null );
				}

				if ( bookingMapBoolean == true
						|| ( curSessionBookingMapBoolean != null && curSessionBookingMapBoolean == true ) ) {
					writer.write( "/true" );
				} else {
					writer.write( "/false" );
				}

			}
			writer.flush();
			writer.close();

		} catch ( FileNotFoundException e ) {
			Log.e( TAG, "Couldn't open the file ", e );
		} catch ( IOException e ) {
			Log.e( TAG, "Did not find raw resource ", e );
		} finally {
			try {
				if ( writer != null ) {
					writer.close();
				}
			} catch ( IOException e ) {
				Log.e( TAG, "Crap ", e );
			}
		}

	}

	public static HashMap<Integer, Boolean> readSeatMap( final Context context, final int seatsCount ) {

		Log.d( TAG, "readSeatMap()  seatsCount =  " + seatsCount );

		HashMap<Integer, Boolean> bookingMap = new HashMap<Integer, Boolean>();
		try {
			File file = new File( context.getCacheDir(), "seatmap" + sCinemaId.toString() );
			FileInputStream mapFileInputStream = new FileInputStream( file );
			BufferedReader reader = new BufferedReader( new InputStreamReader( mapFileInputStream ), 2048 );

			String line = reader.readLine();

			String[] splitLine = line.split( "/" );
			Log.d( TAG, "splitLine: " + Arrays.toString( splitLine ) );
			for ( int i = 0; i < splitLine.length - 1; i++ ) {
				bookingMap.put( i, new Boolean( splitLine[i + 1] ) );
			}

		} catch ( FileNotFoundException e ) {
			Log.e( TAG, "Did not find the file ", e );
			for ( int i = 0; i < seatsCount; i++ ) {
				bookingMap.put( i, false );
			}

			Iterator<Integer> iterator = bookingMap.keySet().iterator();

			while ( iterator.hasNext() ) {
				Integer key = iterator.next();
				Boolean seatBooked = bookingMap.get( key );

				Log.d( TAG, "key =  " + key + " seatBooked = " + seatBooked );
			}
			return bookingMap;

		} catch ( IOException e ) {
			Log.e( TAG, "Couldn't open the file ", e );
			for ( int i = 0; i < seatsCount; i++ ) {
				bookingMap.put( i, false );
			}
			return bookingMap;
		}

		return bookingMap;
	}
}
